<template>
  <div id="addPage" class="container">
    <h1 class="title">添加联系人</h1>
    <form>
      <label for="name">
        <span>姓名</span>
        <input
          type="text"
          placeholder="请输入姓名"
          autocomplete="off"
          v-model="name">
      </label>
      <label for="tel">
        <span>电话号码</span>
        <div class="input_wrap">
          <input
            type="text"
            placeholder="请输入电话号码"
            autocomplete="off"
            :class="{ 'error': telCheck }"
            v-model="tel"
            @blur="input">
          <em v-show="telCheck">*不能为空且全部为数字组成</em>
        </div>
      </label>
      <label for="relationship">
        <span>与我的关系</span>
        <select v-model="status">
          <option selected>朋友</option>
          <option>亲人</option>
          <option>同学</option>
        </select>
      </label>
    </form>
    <div class="button">
      <button
        :disabled="telCheck"
        :class="{ 'disb': telCheck }"
        @click.prevent.stop="add">
        添<span></span>加
      </button>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  name: 'addPage',
  data() {
    return {
      name: '',
      tel: '',
      status: '朋友'
    }
  },
  methods: {
    ...mapActions([ 'userAdd' ]),
    input() {
      // if(this.tel == '') {
      //   this.tel = '！'
      // }
    },
    add() {
      let name = this.name
      if(this.name == '') {
        let num = JSON.parse(localStorage.books).filter(item => {
          return item.status == this.status
        }).length
        name = this.status + (num+1)
      }
      this.userAdd({
        name,
        tel: this.tel,
        status: this.status
      })
      alert('添加成功！')
      this.name = ''
      this.tel = ''
    }
  },
  computed: {
    telCheck() {
      return this.tel.length < 1 || /\D/g.test(this.tel)
    }
  }
}
</script>

<style lang="less" scoped>
#addPage {
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow: hidden;
  h1 {
    font-size: 38px;
    margin-top: 40px;
    margin-bottom: 20px;
  }
  @media screen and (max-width: 600px){
    h1 {
      margin-top: 20px;
    }
  }

  form {
    padding: 20px;
    display: table;
    label {
      width: 445px;
      display: table-row;
      span {
        font-size: 16px;
        text-align: right;
        width: 87px;
        margin-right: 10px;
        display: table-cell;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }
      input {
        width: 300px;
        height: 40px;
        font-size: 16px;
        border-radius: 5px;
        border: 1px solid #ccc;
        background-color: #fff;
        padding: 0 10px;
        margin-left: 10px;
        margin-bottom: 15px;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }
      input.error {
        border: 2px solid red;
      }
      em {
        color: red;
      }
      .input_wrap {
        display: flex;
        flex-direction: column;
      }
      select {
        width: 150px;
        height: 35px;
        border: 1px solid #ccc;
        border-radius: 5px;
        margin-left: 10px;
        margin-top: 10px;
        padding-left: 10px;
      }
      select:focus {
        outline: none;
      }
      @media screen and (max-width: 600px){
        span,
        input {
          font-size: 13px;
        }
        span {
          width: 70px;
        }
        input {
          width: 150px;
          height: 30px;
        }
        select {
          width: 100px;
          height: 25px;
          font-size: 13px;
        }
      }
      @media screen and (max-width: 320px){
        span {
          width: 50px;
        }
        input {
          width: 100px;
        }
        select {
          width: 80px;
        }
      }
    }
    @media screen and (min-width: 1024px){
      label {
        width: 685px;
      }
    }
    @media screen and (max-width: 600px){
      label {
        width: 245px;
      }
    }
    @media screen and (max-width: 320px){
      label {
        width: 195px;
      }
    }
  }
  .button {
    button {
      width: 150px;
      height: 40px;
      border: 0;
      border-radius: 5px;
      background-color: #0f88eb;
      font-size: 18px;
      color: #fff;
      cursor: pointer;
      span {
        width: 2px;
        display: inline-block;
      }
    }
    button.disb {
      background-color: #ccc;
    }
  }
}
</style>
